/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.util;

import org.eclipse.core.resources.IFile;
import org.eclipse.debug.ui.console.FileLink;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * <b>Doesn't work</b>; this is supposed to be a link to a file
 * where only row/column information is known (instead of the absolute
 * offset).  It's based on FileLink to use some helper methods. 
 * @author Kevin Bierhoff
 *
 */
public class FileRowColLink extends FileLink {

	private int row;
	private int col;
	private int len;

	public FileRowColLink(IFile file, String editorId, 
			int fileLineNumber, int fileColumn, int fileLength) {
		super(file, editorId, -1, fileLength, fileLineNumber);
		this.row = fileLineNumber;
		this.col = fileColumn;
		this.len = fileLength;
	}

	/**
	 * Returns the currently active workbench window or <code>null</code>
	 * if none.
	 * 
	 * @return the currently active workbench window or <code>null</code>
	 */
	public static IWorkbenchWindow getActiveWorkbenchWindow() {
		return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.console.IConsoleHyperlink#linkActivated()
	 */
	public void linkActivated() {
//		if(row <= 0 || col <= 0 || len < 0) {
			super.linkActivated();
//			return;
//		}
//			
//		IWorkbenchWindow window = getActiveWorkbenchWindow();
//		if (window != null) {
//			IWorkbenchPage page = window.getActivePage();
//			if (page != null) {
//				try {
//					IEditorPart editorPart = page.openEditor(new FileEditorInput(fFile), getEditorId() , true);
//					if (fFileLineNumber > 0) {
//						ITextEditor textEditor = null;
//						if (editorPart instanceof ITextEditor) {
//							textEditor = (ITextEditor) editorPart;
//						} else {
//							textEditor = (ITextEditor) editorPart.getAdapter(ITextEditor.class);
//						}
//						if (textEditor != null) {
//							IEditorInput input = editorPart.getEditorInput();
//							if (fFileOffset < 0) {
//								IDocumentProvider provider = textEditor.getDocumentProvider();
//								try {
//									provider.connect(input);
//								} catch (CoreException e) {
//									// unable to link
//									DebugUIPlugin.log(e);
//									return;
//								}
//								IDocument document = provider.getDocument(input);
//								try {
//	                                IRegion region= document.getLineInformation(fFileLineNumber - 1);
//									fFileOffset = region.getOffset();
//									fFileLength = region.getLength();
//								} catch (BadLocationException e) {
//									// unable to link
//									DebugUIPlugin.log(e);
//								}
//								provider.disconnect(input);
//							}
//							if (fFileOffset >= 0 && fFileLength >=0) {
//								textEditor.selectAndReveal(fFileOffset, fFileLength);
//							}
//						}
//					}
//				} catch (PartInitException e) {
//					DebugUIPlugin.log(e);
//				}	
//			}
//		}
	}

}
